<template>
    <view>
        <!-- pages/address/add.wxml -->
        <!-- <header name="添加地址" type="1"></header> -->

        <view class="addr">
            <text>收货人：</text>
            <input type="text" placeholder="输入收货姓名" v-model="addr.name"  data-name="name" placeholder-style="color:rgba(36, 44, 54, 0.3);" />
        </view>
        <view class="addr">
            <text>电话：</text>
            <input type="text" placeholder="输入联系电话" v-model="addr.phone"  data-name="phone" placeholder-style="color:rgba(36, 44, 54, 0.3);" />
        </view>
        <view class="addr">
            <text>所在地：</text>
            <pickerAddress @change="change">
				<input type="text" placeholder="请选择地区" v-model="txt" disabled placeholder-style="color:rgba(36, 44, 54, 0.3); width:65%" /></pickerAddress>
        </view>
        <view class="addr">
            <text>详细地址：</text>
            <input
                type="text"
                placeholder="楼号、楼层、单号门牌号等"
                v-model="addr.address"
               
                data-name="address"
                style="font-size: 28rpx; width: 67%"
                placeholder-style="color:rgba(36, 44, 54, 0.3);"
            />
        </view>
        <!-- <view class="moren">
            <text>设为默认地址</text>
            <switch color="$main_color" @change="changemoren" :checked="addr.isdefault == 1"></switch>
        </view> -->
        <view class="addrbtn" @tap="baocun">{{ id ? '保存' : '添加地址' }}</view>
    </view>
</template>

<script>
// pages/address/add.js
import pickerAddress from '../../components/pickerAddress.vue'
var reg = /\S/;
var myreg = /^[1][0-9][0-9]{9}$/;
export default {
	components: {
		pickerAddress
	},
    data() {
        return {
            addr: {
                name: '',
                phone: '',
                sheng: '',
                shi: '',
                qu: '',
                zhen: '',
                address: '',
                lng: '',
                lat: '',
                region: [],
                isdefault: 0
            },
			txt:'请选择地区',
            id: ''
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
       
        if (options.id) {
			this.id= options.id
        }

        if (options.id) {
            this.init();
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    // onShareAppMessage: function () {
    // }
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */ methods: {
        init() {
            var _this = this;

            this.$http(
                'center/getaddr',
                {
                    id: this.id
                },
                function (res) {
                    _this.addr= res.data;
                }
            );
        },
			change(data) {
                this.txt = data.data.join('-')
                this.addr.sheng = data.data[0];
				this.addr.shi = data.data[1];
				this.addr.qu = data.data[2];
            },
        changemoren(e) {
            this.addr.isdefault = e.detail.value;
            
        },

        openlatlng() {
            var _this = this;

            uni.chooseLocation({
                success(res) {
                    _this.addr.lat = res.latitude;
                    _this.addr.lng = res.longitude;
                    _this.addr.sheng = getarea.getArea(res.address).Province;
                    _this.addr.shi = getarea.getArea(res.address).City;
                    _this.addr.qu = getarea.getArea(res.address).Country;
                    _this.addr.address = res.address.replace(_this.addr.sheng + _this.addr.shi + _this.addr.qu, '');

                    
                },

                fail(res) {
                    if (res.errMsg == 'chooseLocation:fail auth deny') {
                        uni.showModal({
                            title: '提示',
                            content: '打开定位失败，请授权使用定位功能',
                            complete: function (res) {
                                if (res.confirm) {
                                    uni.openSetting({});
                                } else {
                                }
                            }
                        });
                    }
                }
            });
        },

        /**地区选择 */
        bindRegionChange: function (e) {
            var that = this;
            that.addr.sheng = e.detail.value[0];
            that.addr.shi = e.detail.value[1];
            that.addr.qu = e.detail.value[2];
            
        },

        xgxx(e) {
            var name = e.currentTarget.dataset.name;
            this.addr[name] = e.detail.value;
            
        },

        baocun: function () {
            var that = this;

            if (!reg.test(that.addr.name)) {
                uni.showToast({
                    title: '请输入收件人姓名',
                    icon: 'none',
                    duration: 2000
                });
            } else if (!myreg.test(that.addr.phone)) {
                uni.showToast({
                    title: '请输入正确的联系电话',
                    icon: 'none',
                    duration: 2000
                });
            } else if (!that.addr.sheng) {
                uni.showToast({
                    title: '请选择所在地区',
                    icon: 'none',
                    duration: 2000
                });
            } else if (!reg.test(that.addr.address)) {
                uni.showToast({
                    title: '请输入详细地址',
                    icon: 'none',
                    duration: 2000
                });
            } else {
                uni.showLoading({
                    title: '新增中',
                    mask: true
                });
                that.$http(
                    'center/addaddress',
                    {
                        id: that.addr.id,
                        name: that.addr.name,
                        phone: that.addr.phone,
                        sheng: that.addr.sheng,
                        shi: that.addr.shi,
                        qu: that.addr.qu,
                        address: that.addr.address,
                    },
                    function (res) {
                        if (res.code == 0) {
                            uni.showToast({
                                title: res.msg,

                                complete() {
                                    setTimeout(() => {
                                        uni.navigateBack({
                                            delta: 1
                                        });
                                    }, 1200);
                                }
                            });
                        } else {
                            uni.showToast({
                                title: res.msg,
                                icon: 'error'
                            });
                        }
                    }
                );
            }
        }
    }
};
</script>
<style lang="scss">
@import './add.scss';
</style>
